﻿using iMES.Core.CacheManager;
using iMES.Core.Utilities;
using iMES.Custom.IRepositories;
using iMES.Entity.DomainModels;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace iMES.Custom.Services
{
    public partial class Produce_OrderListService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IProduce_OrderListRepository _repository;//访问数据库
        private readonly IBase_NumberRuleRepository _numberRuleRepository;//自定义编码规则访问数据库
        private readonly ICacheService _cacheService;//缓存服务

        [ActivatorUtilitiesConstructor]
        public Produce_OrderListService(IHttpContextAccessor httpContextAccessor, IProduce_OrderListRepository repository, IBase_NumberRuleRepository numberRuleRepository, ICacheService cacheService)
            :base(repository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = repository;
            _numberRuleRepository = numberRuleRepository;
            _cacheService = cacheService;
        }
        WebResponseContent webResponse = new WebResponseContent();
        public override PageGridData<Produce_OrderList> GetPageData(PageDataOptions options)
        {
            string cacheKey = "Produce_OrderList:ALL";
            string lockKey = "Produce_OrderList:ALL:LOCK";
            var cacheData = _cacheService.Get<List<Produce_OrderList>>(cacheKey);

            if (cacheData == null)
            {
                // 简单分布式锁实现，防止并发
                bool lockAcquired = false;
                try
                {
                    // 尝试加锁，设置短暂过期时间防止死锁
                    lockAcquired = _cacheService.AddObject(lockKey, "1", 10);
                    if (lockAcquired)
                    {
                        int rowCount;
                        cacheData = _repository.QueryByPage(
                            options.Page,
                            options.Rows,
                            out rowCount,
                            x => true, // 查询所有
                            null       // 不排序
                        );
                        _cacheService.AddObject(cacheKey, cacheData, 60 * 60); // 缓存1小时
                    }
                    else
                    {
                        // 等待锁释放后再取缓存
                        int retry = 0;
                        while (retry < 10 && (cacheData = _cacheService.Get<List<Produce_OrderList>>(cacheKey)) == null)
                        {
                            System.Threading.Thread.Sleep(100);
                            retry++;
                        }
                    }
                }
                finally
                {
                    if (lockAcquired)
                    {
                        _cacheService.Remove(lockKey);
                    }
                }
            }
            return base.GetPageData(options);
        }
    }
}
